const crypto = require('crypto');

// 生成SHA1签名
function genSHA1(token, timestamp, nonce) {
  const arr = [token, timestamp, nonce].sort();
  const str = arr.join('');
  const sha1 = crypto.createHash('sha1');
  return sha1.update(str).digest('hex');
}

// 验证微信签名
function checkSignature(token, signature, timestamp, nonce) {
  const genSign = genSHA1(token, timestamp, nonce);
  return genSign === signature;
}

module.exports = {
  genSHA1,
  checkSignature
};